package com.gelpag;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

//springboot的启动类，必须要添加。以后所有的内容必须在当前包下面，要不spring无法扫描到，不会加载到容器中
@SpringBootApplication
//注册为eureka客户端
@EnableEurekaClient
//作为网关
@EnableZuulProxy
//设置mybitas扫描的路径
@MapperScan("com.gelpag")
public class ZuulApplication extends SpringBootServletInitializer {

    public static void main(String[] args) {

        SpringApplication.run(ZuulApplication.class, args);
    }

    /**
     * 解决Nginx部署vue后，header丢失问题。zuul转发url给微服务header丢失
     * @return
     */
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsFilter(source);
    }
}
